package com.example.smartcitydemo.repo.home

import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.base.BaseRepo
import com.example.smartcitydemo.bean.*
import com.example.smartcitydemo.network.client.authApiService
import com.example.smartcitydemo.network.client.noAuthApiService
import kotlinx.coroutines.flow.Flow

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.repo
 * @NAME: Caleb.F
 * @DATE: 2021/11/4 16:39
 * @Email: 1919796283@qq.com
 * 主页数据仓库
 */
object HomePageRepo : BaseRepo() {
    /**
     * 获取服务类型
     * @return Flow<Resource<List<NewsTypBean>>>
     */
    suspend fun getNewsType(): Flow<Resource<List<NewsTypBean>>> {
        return request({ noAuthApiService.getNewsType() }, {
            it
        })
    }

    /**
     * 获取主页轮播图
     * @return Flow<Resource<List<HomeBannerBean>>>
     */
    suspend fun getHomeBanner(): Flow<Resource<List<HomeBannerBean>>> {
        return requestListData({ noAuthApiService.getHomeBanner() }, {
            it
        })
    }

    /**
     * 获取全部服务
     * @return Flow<Resource<List<ServiceBean>>>
     */
    suspend fun getServiceData(): Flow<Resource<List<ServiceBean>>> {
        return requestListData(
            { noAuthApiService.getServiceList() }, {
                it
            }
        )
    }

    /**
     * 获取新闻列表
     */
    suspend fun getNewsInfo(typeId: Int): Flow<Resource<List<NewsInfo>>> {
        return requestListData({ noAuthApiService.getNewsInfo(typeId) }, { it })
    }

    /**
     * 获取新闻详情
     */
    suspend fun getNewsDetail(newDetailId: Int): Flow<Resource<NewsInfo>> {
        return request({ noAuthApiService.getNewsDetail(newDetailId) }, { it })
    }

    /**
     * 点赞新闻
     * @param articleId Int
     * @return Flow<Resource<String>>
     */
    suspend fun doLikeArticle(articleId: Int): Flow<Resource<String>> {
        return requestCommonData { authApiService.doLikeArticle(articleId) }
    }

    /**
     * 登录
     * @param loginBean LoginBean
     * @return Flow<Resource<String>>
     */
    suspend fun doLogin(loginBean: LoginBean): Flow<Resource<String>> {
        return requestCommonData { noAuthApiService.doLogin(loginBean) }
    }

    /**
     * 获取新闻评论
     *
     */
    suspend fun getNewsReply(newsId: Int): Flow<Resource<List<ReplyInfo>>> {
        return requestListData({ noAuthApiService.getNewsReplyList(newsId) }, { it })
    }

    /**
     * 品论点赞
     */
    suspend fun doLikeCommit(commitId: Int): Flow<Resource<String>> {
        return requestCommonData {
            authApiService.doLikeCommit(commitId)
        }
    }

    /**
     * 新增评论
     */
    suspend fun submitCommit(newsCommitBean:NewsCommitBean): Flow<Resource<String>> {
        return requestCommonData { authApiService.commitReply(newsCommitBean)}
    }

    /**
     * 模糊搜索新闻
     */
    suspend fun getNewsBySearch(title:String): Flow<Resource<List<NewsInfo>>> {
        return requestListData({noAuthApiService.getSearchResult(title)},{it})
    }
}